﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace DungeonMaker
{
    class LoadSave
    {
        string path = "C:\\Users\\holmgrenjohan.rodah\\Desktop\\TextDungeon\\TextDungeon\\Dungeons";
        public LoadSave() { }

        public void SavePlayer( string file)
        {
            file = path +"\\Characters\\"+ file;
            using (BinaryReader read = new BinaryReader(File.Open(file, FileMode.Create)))
            {
                
            }
        }

        public void SaveData(List<Entity> content, string file) 
        {
            file = path + file;

            using (BinaryWriter writer = new BinaryWriter(File.Open(file, FileMode.Create)))
                {
                    foreach (var e in content)
                    {
                        writer.Write(e.symbol);
                        writer.Write(e.pos.X);
                        writer.Write(e.pos.Y);
                        writer.Write((byte) e.type);

                    }
                }
            }


        public List<Entity> LoadData(string file) 
        {
            var temp = new List<Entity>();
            file = path + file;
            int pos = 0;
            if (File.Exists(file))
            {
                using (BinaryReader reader = new BinaryReader(File.Open(file, FileMode.Open)))
                {
                    while (reader.BaseStream.Length > pos)
                    {
                        temp.Add(new Entity(reader.ReadChar(), new Position(reader.ReadInt32(), reader.ReadInt32()),
                                            (EntityType) reader.ReadByte()));

                        pos += (sizeof (char) + (sizeof (Int32)*2) + sizeof (byte));
                    }
                }
            }
            return temp;
        }
    }
}
